package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlSsxsjlbBsDetail;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlSsxsjlbBsDetailVo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlSsxsjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlSsxsjlbBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlSsxsjlbBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlSsxsjlbBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlSsxsjlbBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlSsxsjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料渗水系数试验检测记录Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlSsxsjlbBsInfoServiceImpl implements IReportLqhhlSsxsjlbBsInfoService {

    private final ReportLqhhlSsxsjlbBsInfoMapper baseMapper;
    private final ReportLqhhlSsxsjlbBsDetailMapper detailMapper;

    /**
     * 查询沥青混合料渗水系数试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlSsxsjlbBsInfoVo queryById(Long excelId) {
        ReportLqhhlSsxsjlbBsInfoVo reportLqhhlSsxsjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLqhhlSsxsjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlSsxsjlbBsDetail>().eq(ReportLqhhlSsxsjlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            reportLqhhlSsxsjlbBsInfoVo.setDetailList(detailList);
        }
        return reportLqhhlSsxsjlbBsInfoVo;
    }

    /**
     * 查询沥青混合料渗水系数试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportLqhhlSsxsjlbBsInfoVo> queryPageList(ReportLqhhlSsxsjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlSsxsjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlSsxsjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料渗水系数试验检测记录列表
     */
    @Override
    public List<ReportLqhhlSsxsjlbBsInfoVo> queryList(ReportLqhhlSsxsjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlSsxsjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlSsxsjlbBsInfo> buildQueryWrapper(ReportLqhhlSsxsjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlSsxsjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlSsxsjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlSsxsjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlSsxsjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqhhlSsxsjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlSsxsjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlSsxsjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlSsxsjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlSsxsjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlSsxsjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlSsxsjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlSsxsjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqhhlSsxsjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqhhlSsxsjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqhhlSsxsjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlSsxsjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlSsxsjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlSsxsjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqhhllx()), ReportLqhhlSsxsjlbBsInfo::getLiqhhllx, bo.getLiqhhllx());
        lqw.eq(StringUtils.isNotBlank(bo.getShejssxs()), ReportLqhhlSsxsjlbBsInfo::getShejssxs, bo.getShejssxs());
        lqw.eq(bo.getShensxscdz() != null, ReportLqhhlSsxsjlbBsInfo::getShensxscdz, bo.getShensxscdz());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlSsxsjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlSsxsjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlSsxsjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlSsxsjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlSsxsjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlSsxsjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlSsxsjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlSsxsjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料渗水系数试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlSsxsjlbBsInfoBo bo) {
        ReportLqhhlSsxsjlbBsInfo add = BeanUtil.toBean(bo, ReportLqhhlSsxsjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLqhhlSsxsjlbBsDetail> details = new ArrayList<>(3);
            for (int i = 1; i <= 3; i++) {
                details.add(new ReportLqhhlSsxsjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料渗水系数试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqhhlSsxsjlbBsInfoBo bo) {
        ReportLqhhlSsxsjlbBsInfo update = BeanUtil.toBean(bo, ReportLqhhlSsxsjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportLqhhlSsxsjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlSsxsjlbBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料渗水系数试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
